﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using eShop.DataAccess.Entity;

namespace eShop.DataAccess
{
    public partial class StateDAL : BaseDAL
    {
        public static bool Insert(State state)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("State_InsertOrUpdate");
            db.AddOutParameter(dbCommand, "Id", DbType.Int32, 4);
            db.AddInParameter(dbCommand, "CountryId", DbType.Int32, state.CountryId);
            db.AddInParameter(dbCommand, "Name", DbType.String, state.Name);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, state.IsActive);

            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                state.Id = Convert.ToInt32(db.GetParameterValue(dbCommand, "@Id"));
                return true;
            }
            return false;
        }

        public static bool Update(State state)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("State_InsertOrUpdate");
            db.AddInParameter(dbCommand, "Id", DbType.Int32, state.Id);
            db.AddInParameter(dbCommand, "CountryId", DbType.Int32, state.CountryId);
            db.AddInParameter(dbCommand, "Name", DbType.String, state.Name);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, state.IsActive);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        public static List<State> GetAll(Boolean allowPaging = false, Int32 pageIndex = 0, Int32 pageSize = 0, String orderColumn = "Id", Boolean isAscending = false)
        {
            List<State> stateList = new List<State>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("State_GetAll");
            db.AddInParameter(dbCommand, "AllowPaging", DbType.Boolean, allowPaging);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbCommand, "OrderColumn", DbType.String, orderColumn);
            db.AddInParameter(dbCommand, "IsAscending", DbType.Boolean, isAscending);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                stateList = GetEntitiesFromReader(dataReader);
            }
            return stateList;
        }

        public static List<State> GetByKeys(Int32? id, Int32? countryId)
        {
            List<State> stateList = new List<State>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("State_GetByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (countryId.HasValue)
                db.AddInParameter(dbCommand, "CountryId", DbType.Int32, countryId);

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                stateList = GetEntitiesFromReader(dataReader);
            }
            return stateList;
        }

        public static bool DeleteByKeys(Int32? id, Int32? countryId)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("State_DeleteByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (countryId.HasValue)
                db.AddInParameter(dbCommand, "CountryId", DbType.Int32, countryId);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        private static List<State> GetEntitiesFromReader(IDataReader dataReader)
        {
            List<State> stateList = new List<State>();
            while (dataReader.Read())
            {
                stateList.Add(GetEntityFromReader(dataReader));
            }
            return stateList;
        }

        private static State GetEntityFromReader(IDataReader dataReader)
        {
            State state = new State();
            state.Id = (Int32)dataReader["Id"];
            state.CountryId = (Int32)dataReader["CountryId"];
            state.Name = (String)dataReader["Name"];
            state.IsActive = (Boolean)dataReader["IsActive"];
            return state;
        }
    }
}

